package com.wd.tech.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.wd.tech.R;
import com.wd.tech.base.BaseActivity;
import com.wd.tech.base.BasePresenter;
import com.wd.tech.fragment.SheQuFragment;
import com.wd.tech.fragment.XiaoXiFragment;
import com.wd.tech.fragment.ZiXunFragment;
import com.wd.tech.mvp.IPresenterImpl;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class TabActivity extends BaseActivity {

    @BindView(R.id.home_vp)
    ViewPager homeVp;
    @BindView(R.id.home_rg)
    RadioGroup homeRg;
    @BindView(R.id.drawerlayout)
    DrawerLayout drawerlayout;
    @BindView(R.id.tv_login)
    TextView tvLogin;
    private ArrayList<Fragment> list = new ArrayList<>();

    @Override
    protected void initData() {
        tvLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(TabActivity.this,LoginActivity.class);
                startActivity(intent);
                finish();
            }
        });
        drawerlayout.addDrawerListener(new DrawerLayout.SimpleDrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                super.onDrawerSlide(drawerView, slideOffset);
                //获得侧滑菜单的宽度
                int measuredWidth = drawerlayout.getMeasuredWidth();
                //根据滑动百分比计算内容部分应该向右边移动的距离
                int marginLeft = (int) (measuredWidth * slideOffset);
                View childAt = drawerlayout.getChildAt(0);
                childAt.setLeft(marginLeft);
            }
        });
        list.add(new ZiXunFragment());
        list.add(new XiaoXiFragment());
        list.add(new SheQuFragment());

        //viewPager滑动切换
        homeVp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return list.get(position);
            }

            @Override
            public int getCount() {
                return list.size();
            }
        });

        homeVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                homeRg.check(homeRg.getChildAt(position).getId());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void initView() {
        homeRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {
                    case R.id.homg_rb01:
                        homeVp.setCurrentItem(0);
                        break;
                    case R.id.homg_rb02:
                        homeVp.setCurrentItem(1);
                        break;
                    case R.id.homg_rb03:
                        homeVp.setCurrentItem(2);
                        break;
                }
            }
        });
    }

    @Override
    protected BasePresenter initPresenter() {
        return new IPresenterImpl();
    }

    @Override
    protected int layoutId() {
        return R.layout.tab_main;
    }

    @Override
    public void success(Object o) {

    }

    @Override
    public void Error(Throwable throwable) {

    }



}
